package com.erp.module.gongGao

import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.AdapterView
import com.erp.R
import com.erp.activity.GonggaoTextActivity
import com.erp.bean.GonggaoBean
import com.erp.bean.Twostringbean
import com.erp.contacts.Contacts
import com.erp.myAdapter.ArrayAdapter
import com.erp.myAdapter.GonggaoAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.fragment_zero.*
import java.util.*

/**
 * Created by Administrator on 2017/1/10/010.
 * 作者：刘洋
 * 公告页面
 */
class FragmentGongGao : BaseKFragment(), AdapterView.OnItemClickListener, PullToRefreshView.OnFooterRefreshListener, PullToRefreshView.OnHeaderRefreshListener, View.OnClickListener,GongGaoContract.GongGaoView {



    lateinit var listgonggao: MutableList<GonggaoBean>
    lateinit var adapter: GonggaoAdapter
    lateinit var searchParams: MutableList<Twostringbean>
    var updatedialog: AlertDialog? = null
    lateinit var P : IGongGaoP

    override fun getLayoutId(): Int {
        fragmentname = f
        return R.layout.fragment_zero
    }


    override fun initData() {
        searchParams = ArrayList()
        listgonggao = ArrayList()
        P=IGongGaoP(listgonggao,context!!,this)
        adapter = GonggaoAdapter(activity!!, listgonggao, Date().time / 1000)
        LvZero.adapter = adapter
        LvZero.onItemClickListener = this
        Pull.setheader(true)
        Pull.setOnFooterRefreshListener(this)
        Pull.setOnHeaderRefreshListener(this)
        IvGonggao.setOnClickListener(this)
        EtGonggao.setOnEditorActionListener { v, actionId, event ->
            var isOK = true
            when (actionId) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    sousuo()
                }
                else -> isOK = false
            }
            isOK
        }
        P.gainData("GongGao")
        Pull.headerRefreshing()
        initsp()
    }

    fun qingkong(){
        if(isFirstLoad){
            return
        }
        Pull.headerRefreshing()
    }

    override fun xialaOk() {
        updatedialog?.dismiss()
        Pull.onHeaderRefreshComplete()
        adapter.Allnoti(listgonggao)
    }

    override fun xialaNo() {
        updatedialog?.dismiss()
        Pull.onHeaderRefreshComplete()
    }

    override fun shanglaOk() {
        updatedialog?.dismiss()
        Pull.onFooterRefreshComplete()
        adapter.Allnoti(listgonggao)
    }

    override fun shanglaNo() {
        updatedialog?.dismiss()
        Pull.onFooterRefreshComplete()
    }
    override fun gainDataOk() {
        adapter.Allnoti(listgonggao)
    }

    override fun onFooterRefresh(view: PullToRefreshView) {
        P.shangla()
    }
    override fun onHeaderRefresh(view: PullToRefreshView) {
        P.xiala()
    }

    private fun initsp() {
        val listsp = ArrayList<String>()
        listsp.add("全部")
        listsp.add("生产中心")
        listsp.add("营销中心")
        val spadapter = ArrayAdapter(activity, R.layout.lv_sp_tvitem, listsp)
        SpGonggao.adapter = spadapter
    }

    override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
        val intent = Intent(activity, GonggaoTextActivity::class.java)
        intent.putExtra("code", 1)
        intent.putExtra("url", Contacts.GONGGAOXX + listgonggao[position].nid + "?mobile=1")
        intent.putExtra("title", "公  告")
        startActivity(intent)
    }



    override fun onClick(v: View) {
        when (v) {
            IvGonggao -> {
                sousuo()
            }
        }
    }

    fun sousuo(){
        searchParams.clear()
        val t = Twostringbean()
        if (SpGonggao.selectedItem.toString() != "全部") {
            t.name = "notice_type"
            if (SpGonggao.selectedItem.toString() == "生产中心") {
                t.string = "production_center_notice"
            } else if (SpGonggao.selectedItem.toString() == "营销中心") {
                t.string = "sales_center_notice"
            } else {
                t.string = ""
            }
            searchParams.add(t)
        }
        if (EtGonggao.text.toString().trim() != "") {
            t.name = "notice_title"
            t.string = EtGonggao.text.toString()
            searchParams!!.add(t)
        }
        updatedialog = Myutil.showupdatedialog(activity, "正在搜索", false)
        P.sousuo(searchParams)
    }

    companion object {
        private val f = "FragmentZero"
    }
}
